﻿using System;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLKDS.DAL;
using DevExpress.XtraEditors.DXErrorProvider;
using DevExpress.XtraEditors.Controls;
using QLKDS.DTO;

namespace QLKDS.Forms
{
    public partial class frmBookAdd : XtraForm
    {
        #region Properties
        private BooksManager bookManager;
        private BookStoreManager bookStoreManager=new BookStoreManager();
        private StoreManager storeManager = new StoreManager();
        private CategoryManager categoryManager = new CategoryManager();
        private AuthorManager authorManager = new AuthorManager();
        private PublisherManager publisherManager = new PublisherManager();
        private frmBook Form;
        #endregion

        #region Methods

        private void SetMaxLengthOfTextBoxes()
        {
            this.txtName.Properties.MaxLength = 32;
            this.txtCode.Properties.MaxLength = 8;
            this.txtDiscountRetail.Properties.MaxLength = 16;
            this.txtPrice.Properties.MaxLength = 16;            
        }

        /// <summary>
        /// Clear all Textboxes on this form
        /// </summary>
        private void ClearTextBoxes()
        {
            this.txtCode.Text = String.Empty;
            this.txtName.Text = String.Empty;
            this.txtDiscountRetail.Text = String.Empty;
            this.txtPrice.Text = String.Empty;
            this.txtDescription.Text = String.Empty;
            this.dteCreatedDate.DateTime = DateTime.Now;
            this.dteCreatedDate.Properties.MaxValue = DateTime.Now;
            this.txtName.Focus();
        }
        #endregion

        #region Init form
        public frmBookAdd(frmBook form)
        {
            InitializeComponent();
            this.bookManager = new BooksManager();
            this.Load_info();
            this.Form = form;
           

        }

        public void Load_info()
        {

            this.ccbeAuthors.Properties.DataSource = authorManager.ListAuthor();
            this.ccbeAuthors.Properties.DisplayMember = "Name";
            this.ccbeAuthors.Properties.ValueMember = "Id";

            this.ccbeCategory.Properties.DataSource = categoryManager.ListCategory();
            this.ccbeCategory.Properties.DisplayMember = "Name";
            this.ccbeCategory.Properties.ValueMember = "Id";

            this.luePublisher.Properties.DataSource = publisherManager.ListPublisher();
            this.luePublisher.Properties.DisplayMember = "Name";
            this.luePublisher.Properties.ValueMember = "Id";
        }

        private void ccbeAuthors_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            ButtonEdit editor = (ButtonEdit)sender;
            EditorButton Button = e.Button;
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Plus)
            {
                var form = new frmAuthorsAdd(this);
                form.ShowDialog();
            }
        }

        private void ccbeCategory_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            ButtonEdit editor = (ButtonEdit)sender;
            EditorButton Button = e.Button;
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Plus)
            {
                var form = new frmCategoryAdd(this);
                form.ShowDialog();
            }
        }

        private void luePublisher_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            ButtonEdit editor = (ButtonEdit)sender;
            EditorButton Button = e.Button;
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Plus)
            {
                var form = new frmPublisherAdd();
                if (form.ShowDialog() == DialogResult.OK)
                {
                    this.Load_info();
                }
            }
        }

        private void frmBookAdd_Load(object sender, EventArgs e)
        {
            this.ClearTextBoxes();
            this.SetMaxLengthOfTextBoxes();
           
        }
        #endregion

        #region Validate

        private void txtName_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtName.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                this.dxepName.ClearErrors();
            }
        }
        private void txtName_Leave(object sender, EventArgs e)
        {
            if (this.txtName.Text.Trim() == String.Empty)
            {
                this.dxepName.SetError(this.txtName, "Tên sách không được để trống.", ErrorType.User1);
            }
        }

        private void txtCode_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtCode.Text.Trim() == String.Empty)
            {
               //
            }
            else
            {
                if (this.bookManager.HasCodeExisted(this.txtCode.Text.Trim()))
                {
                    this.dxepCode.SetError(this.txtCode, "Mã sách " + this.txtCode.Text.Trim() + " đã tồn tại trong hệ thống. Hãy chọn một mã sách khác.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                    this.txtCode.Focus();
                    return;
                }

                this.dxepCode.ClearErrors();
            }
        }
        private void txtCode_Leave(object sender, EventArgs e)
        {
            if (this.txtCode.Text.Trim() == String.Empty)
            {
                this.dxepCode.SetError(this.txtCode, "Mã sách không được để trống.", ErrorType.User1);
            }
        }



    

        private void txtCreatedBy_Leave(object sender, EventArgs e)
        {
            if (this.txtCreatedBy.Text.Trim() == String.Empty)
            {
                this.dxepCreatedBy.SetError(this.txtCode, "Lựa chọn tên nhân viên thêm mới.", ErrorType.User1);
            }
        
        }

        private void txtCreatedBy_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtCreatedBy.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                this.dxepCreatedBy.ClearErrors();
            }
        }

        #endregion

        #region Button_click
        private void btnClear_Click(object sender, EventArgs e)
        {
            this.ClearTextBoxes();
        }
       
        private void btnSave_Click(object sender, EventArgs e)
        {


              //validation name
              if (this.txtName.Text.Trim().Length == 0)
              {
                  this.dxepName.SetError(this.txtName, "Tên sách không được để trống.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                  this.txtName.Focus();
                  return;
              }
              else
              {
                  this.dxepName.ClearErrors();
              }
            //end validation name


            //validation Code
            if (this.txtCode.Text.Trim().Length == 0)
            {
                this.dxepCode.SetError(this.txtCode, "Mã sách không được để trống.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.txtCode.Focus();
                return;
            }
            else if (this.bookManager.HasCodeExisted(this.txtCode.Text.Trim()) == true)
            {
                this.dxepName.SetError(this.txtCode, "Mã sách " + this.txtCode.Text.Trim() + " đã tồn tại trong hệ thống. Hãy chọn một mã sách khác.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.txtName.Focus();
                return;
            }
            else
            {
                this.dxepCode.ClearErrors();
            }
            //end validation Code

            //Validation CreatedBy
            if (this.txtCreatedBy.Text.Trim().Length == 0)
            {
                this.dxepCreatedBy.SetError(this.txtCreatedBy, "Lựa chọn tên nhân viên thêm mới");
                this.txtCreatedBy.Focus();
                return;
            }
            //End validation CreatedBy

            // End of validation
            
            /*****************************************************************/
            // Save book into database                   
            var book = new Book();
            book.Code = this.txtCode.Text.Trim();
            book.Name = this.txtName.Text.Trim();
            book.Description = this.txtDescription.Text.Trim();
            book.Status = 1; 
            book.Price = double.Parse(this.txtPrice.Text.Trim());
            book.DiscountWholesale = double.Parse(this.txtDiscountWholesale.Text.Trim());
            book.DiscountRetail = double.Parse(this.txtDiscountRetail.Text.Trim());
            book.Publisher =this.luePublisher.Text.Trim();
            book.CreatedBy = this.txtCreatedBy.Text;
            book.CreatedDate = this.dteCreatedDate.DateTime;
            book.ModifiedBy = "admin";
            book.ModifiedDate = this.dteCreatedDate.DateTime;

            this.bookManager.CreateBook(book);
            // save book

            //Save bookStore
            var ListStore = this.storeManager.ListStore();

            foreach (StoreInfo store in ListStore)
            {
                var bookStore = new BookStore();
                bookStore.BookId = book.Id;
                bookStore.StoreId = store.Id;
                bookStore.Quantity = 0;
                this.bookStoreManager.CreateBookStore(bookStore);
            }

          
            //var authorBook = new AuthorsBook();
            //authorBook.BookId=this.bookManager.get
         
          

            if ((DialogResult)XtraMessageBox.Show("Dữ liệu thêm thành công.Tiếp tục thêm ?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                ClearTextBoxes();
            }
            else
            {
                this.Close();
            }
            this.Form.PopulateBookGridView();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion





      

 

      

     
        


    }
}
